package testing;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.util.Hashtable;
import java.util.Enumeration;
import DataBase.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;

import cliente_diseno2011.Controller;
import cliente_diseno2011.Interfaz;
import cliente_diseno2011.Modelo;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TestDataBase {
	private JFrame frame;
	private DBManager db;

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestDataBase window = new TestDataBase();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TestDataBase() {
		initialize();
		db = new DBManager();
		db.limpiarDB();
		db.datosPrueba();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 562, 337);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 79, 539, 224);
		frame.getContentPane().add(scrollPane);

		final JTextPane textPane = new JTextPane();
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);

		JButton btnNewButton = new JButton("Ver Votantes");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String conjunto = "";
				Hashtable<String, Votante> votantes = db.getVotantes();
				for (Enumeration<Votante> i = votantes.elements(); i
						.hasMoreElements();) {
					Votante v = (Votante) i.nextElement();
					conjunto = conjunto + v.getPin() + " " + v.getDNI() + " "
							+ v.getApellido() + " " + v.getNombre() + " "
							+ v.getFinalizo() + "\n";
				}
				textPane.setText(conjunto);
			}
		});
		btnNewButton.setBounds(10, 11, 109, 23);
		frame.getContentPane().add(btnNewButton);

		JButton btnNewButton_2 = new JButton("Ver Resultados");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textPane.setText(db.getResultadosParaTransmitir());
			}
		});
		btnNewButton_2.setBounds(129, 11, 121, 23);
		frame.getContentPane().add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("Ver Autoridades");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String conjunto = "";
				Hashtable<String, Autoridad> autoridades = db.getAutoridades();
				for (Enumeration<Autoridad> i = autoridades.elements(); i
						.hasMoreElements();) {
					Autoridad a = (Autoridad) i.nextElement();
					conjunto = conjunto + a.getPin() + " " + a.getDNI() + " "
							+ a.getApellido() + " " + a.getNombre() + " "
							+ a.getPosicion() + "\n";
				}
				textPane.setText(conjunto);
			}
		});
		btnNewButton_3.setBounds(262, 11, 127, 23);
		frame.getContentPane().add(btnNewButton_3);

		JButton btnNewButton_4 = new JButton("Ver Administradores");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String conjunto = "";
				Hashtable<String, Administrador> administradores = db
						.getAdministradores();
				for (Enumeration<Administrador> i = administradores.elements(); i
						.hasMoreElements();) {
					Administrador a = (Administrador) i.nextElement();
					conjunto = conjunto + a.getPin() + " " + a.getDNI() + " "
							+ a.getApellido() + " " + a.getNombre() + " "
							+ a.getTelefono() + "\n";
				}
				textPane.setText(conjunto);
			}
		});
		btnNewButton_4.setBounds(403, 11, 146, 23);
		frame.getContentPane().add(btnNewButton_4);

		JButton btnNewButton_5 = new JButton("Clear DB");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				db.limpiarDB();
				textPane.setText("");
			}
		});
		btnNewButton_5.setBounds(104, 44, 86, 23);
		frame.getContentPane().add(btnNewButton_5);

		JButton btnRomperDb = new JButton("Romper DB");
		btnRomperDb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				db.romperActiva();
			}
		});
		btnRomperDb.setBounds(202, 44, 112, 23);
		frame.getContentPane().add(btnRomperDb);

		JButton btnCargarDb = new JButton("Load DB");
		btnCargarDb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				db = new DBManager();
				db.datosPrueba();
			}
		});
		btnCargarDb.setBounds(10, 44, 82, 23);
		frame.getContentPane().add(btnCargarDb);

		JButton btnIniciarUrna = new JButton("Iniciar urna");
		btnIniciarUrna.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							Interfaz frame = new Interfaz();
							Modelo m = new Modelo(db);
							Controller c = new Controller(frame, m);
							frame.setBounds(300, 300, 400, 400);
							frame.setVisible(true);
							frame.setLocationRelativeTo(null);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		btnIniciarUrna.setBounds(326, 44, 115, 23);
		frame.getContentPane().add(btnIniciarUrna);

		JButton btnVerLog = new JButton("Ver log");
		btnVerLog.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Runtime.getRuntime().exec(
							"notepad.exe ./src/resources/LogFile");
				} catch (Exception ee) {
					textPane.setText("no");
				}
			}
		});
		btnVerLog.setBounds(453, 44, 96, 23);
		frame.getContentPane().add(btnVerLog);
	}
}
